package com.ruifight.data.common;

import java.math.BigInteger;

/**
 * Created by liuhx on 2016/11/23.
 */
public class HexadecimalConversion {
    public static String toStringHex(String s)
    {
        byte[] baKeyword = new byte[s.length()/2];
        for(int i = 0; i < baKeyword.length; i++)
        {
            try
            {
                baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        try
        {
            s = new String(baKeyword, "utf-8");//UTF-16le:Not
        }
        catch (Exception e1)
        {
            e1.printStackTrace();
        }
        return s;
    }
    /**
     *
     * 将十进制转成16进制
     */
        public static String toHex(byte[] b)
        {
            String ret = "";
            for (int i = 0; i < b.length; i++) {
                String hex = Integer.toHexString(b[ i ] & 0xFF);
                if (hex.length() == 1) {
                    hex = '0' + hex;
                }
                ret += hex.toUpperCase()+" ";
            }
            return ret;
        }
    /**
     * 将十六进制转二进制并补全8位
     *
     *
     */
    public static String HexToBinary(String a)
    {
        BigInteger srch = new BigInteger(a, 16);
        String d =Integer.toBinaryString(Integer.parseInt(srch.toString()));
        int z = 8-d.length();
        String zero="";
        if(z>0)
        {
            for(int c=0;c<z;c++)
            {
                zero="0"+zero;
            }
        }
        d=zero+d;
        return d;
    }
    /**
     *
     * @param
     * @return 将二进制转换为十六进制字符输出
     */
    public static int BinaryToHex(String a)
    {
        int c =Integer.getInteger(Integer.toHexString(Integer.valueOf(a,2)));
        return c;
    }
    /**
     * 进行异或解密
     *
     */
    public static String dataDecrypt(String[] b)
    {
        String a="";
        for(int x=0;x<b.length;x++)
        {
            if(null!=b[x]&&!"".equals(b[x])) {
                Integer z = Integer.parseInt(b[x],16)^Integer.parseInt("66",16);
                String sd =Integer.toHexString(z);
                if(sd.length()<2)
                {
                    sd="0"+sd;
                }
                a=a+sd;
            }
        }
        return a;
    }
    /**
     * 十六转十进制组合转换
     */
    public static String hexToDecimal(String hex)
    {
        String a="";
        for(int b=0;b<hex.length();b=b+2)
        {
            String c =Integer.valueOf( hex.substring(b, b+2),16).toString();
            if(c.length()<2)
            {
                c="0"+c;
            }
            a=a+c;
        }
        return a;
    }
    /**
     * 十六转十进制，并生成数组
     */
    public static int[] hexToDecimalInt(String hex){
        int a[] = new int[hex.length()/2];
        int n = 0;
        for(int b=0;b<hex.length()/2;b++)
        {
            int c =Integer.parseInt(hex.substring(n, n+2),16);
            a[b]=c;
            n=n+2;
        }
        return a;
    }
}
